/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.xilin.event

import org.springframework.context.*
import org.springframework.security.event.authentication.*
import org.springframework.security.event.authorization.AbstractAuthorizationEvent

import javax.servlet.http.HttpSession
import org.springframework.beans.factory.InitializingBean
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.RequestAttributes

/**
 *
 * @author gfu00
 */
class AcegiEventListener implements ApplicationListener {

    void onApplicationEvent(final ApplicationEvent e) {
        if (e instanceof AbstractAuthenticationEvent) {
            if (e instanceof AuthenticationSuccessEvent) {
                def session = getSession()
                session.principal = e.source.principal
                session.user = e.source.principal.username
                session.roles = e.source.authorities.collect {it.toString()}
                println "** Attention: User ${e.source.principal.username} is logged"
                println "** session.roles:" + session.roles
                println "** principal: User ${e.source.principal} is logged in"
            }
        }
    }

    private HttpSession getSession() {
        return RequestContextHolder.currentRequestAttributes().getSession()
    }
}

